package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;

import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 用户操作
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    @Reference
    private RoleService roleService;

    /**
     * 查询所有角色
     */
    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<Role> roles = roleService.findAll();
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS,roles);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ROLE_FAIL);
        }
    }
    /**
     * 分页查询
     */
    @RequestMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        try {
            String queryString = queryPageBean.getQueryString();
            Integer currentPage = queryPageBean.getCurrentPage();
            Integer pageSize = queryPageBean.getPageSize();

            PageResult page = roleService.findPage(currentPage, pageSize, queryString);
            return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,page);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ROLE_FAIL);

        }
    }


    /**
     * 新增角色
     */
    @RequestMapping("/add")
    public Result addRole(@RequestBody Role role,Integer[] menuIds,Integer[]  permissionIds){
        try{
            roleService.addRole(role,menuIds,permissionIds);
            return new Result(true,MessageConstant.ADD_ROLE_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_ROLE_FAIL);
        }

    }

    /**
     * 编辑回显数据
     */
    @RequestMapping("/findById")
    public Result getRole( Integer id){

        try{
            Role role = roleService.getRole(id);
            return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,role);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    /**
     * 编辑修改数据
     */
    @RequestMapping("/update")
    public Result updateRole(@RequestBody Role role,Integer[] menuIds,Integer[]   permissionIds){
        try {
            roleService.updateRole(role,menuIds,permissionIds);
            return new Result(true,MessageConstant.EDIT_ROLE_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_ROLE_FAIL);
        }
    }

    /**
     * 删除角色数据
     */
    @RequestMapping("/delete")
    public Result deleteRole(Integer id){

        try {
            roleService.deleteRole(id);
            return new Result(true,MessageConstant.DELETE_ROLE_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_ROLE_FAIL);
        }
    }

}
